package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Emp;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

@Slf4j
@RequestMapping("/emps")
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    // 可选条件的分页查询,条件通过参数存在EmpQueryParam对象中
    @GetMapping
    public Result page(EmpQueryParam empQueryParam) {
        log.info("分页查询:{}", empQueryParam);
        PageResult<Emp> pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    // 批量删除员工
    @Log
    @DeleteMapping
    public Result delete(@RequestParam List<Integer> ids) {
        log.info("批量删除如下id的员工:{}", ids.toString());
        empService.delete(ids);
        return Result.success();
    }

    // 添加员工的基本信息,包含工作经历
    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp) {
        log.info("请求参数emp:{}", emp);
        empService.save(emp);
        return Result.success();
    }

    // 根据路径id查询员工信息
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("查询id为{}的员工信息", id);
        Emp emp = empService.findById(id);
        return Result.success(emp);
    }

    // 修改员工
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp) {
        log.info("修改员工信息:{}", emp);
        empService.update(emp);
        return Result.success();
    }

    // 查询全部员工,不需要工作经历(更改为查询班主任)
    @GetMapping("/list")
    public Result listAll() {
        log.info("输出所有的员工信息");
        List<Emp> empList = empService.listAll();
        return Result.success(empList);
    }

}
